package com.xk.meeting.controller;

import com.xk.meeting.entity.User;
import com.xk.meeting.service.UserService;
import com.xk.meeting.service.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/modifyName")
public class ModifyNameServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf8");
        String username = request.getParameter("username");
        String id = request.getParameter("id");
        UserService userService = new UserServiceImpl();
        int status = userService.updateName(username,Integer.parseInt(id));

        if (status > 0){
            HttpSession session = request.getSession();
            User user = (User)session.getAttribute("user");
            user = userService.getUserForPhone(user.getPhone());
            session.setAttribute("user",user);
            response.sendRedirect("myboard.jsp");
        }else {
            response.sendRedirect("errorPage.jsp");
        }
    }
}
